using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//商品分类
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class CommodityTypeController : ControllerBase
    {
        private IRepository<CommodityClassifications> _typeRepository;

        public CommodityTypeController(
            IRepository<CommodityClassifications> typeRepository
        )
        {
            _typeRepository = typeRepository;

        }

        //添加商品分类
        //Post: /commoditytype
        [HttpPost]
        public dynamic AddCommodityTupe(NewType model)
        {
            var name = model.ClassificationName.Trim();
            var pid = model.ParentLevelId;
            var level = model.ClassLevel;

            var isType = _typeRepository.Table.Where(x => x.ClassificationName == name).FirstOrDefault();

            if (isType == null)
            {
                var type = new CommodityClassifications
                {
                    ClassificationName = name,
                    ClassLevel = level,
                    ParentLevelId = pid
                };

                _typeRepository.Insert(type);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = type,
                    Msg = "添加成功"
                });
            }
            else
            {
                if (isType.IsDelete == true)
                {
                    var type = new CommodityClassifications
                    {
                        ClassificationName = name,
                        ClassLevel = level,
                        ParentLevelId = pid
                    };

                    _typeRepository.Insert(type);

                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = type,
                        Msg = "添加成功"
                    });
                }
                else
                {
                    return JsonHelper.Serialize(new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "当前分类名称已存在"
                    });
                }
            }

        }


        // 获取全部的分类
        // Get /commoditytype
        [HttpGet]
        public dynamic GetAllType()
        {

            var type = _typeRepository.Table.Where(x => x.IsDelete == false).ToList();
            List<CommodityClassifications> allType = ArrangeHelper.GetMenu(0, type);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = allType,
                Msg = "查询成功"
            });
        }


        //修改分类名称
        // Put /commoditytype/id
        [HttpPut("{id}")]
        public dynamic EditType(int id, NewType model)
        {

            var name = model.ClassificationName.Trim();
            var isType = _typeRepository.GetById(id);

            isType.ClassificationName = name;
            isType.UpdateTime = DateTime.Now;

            _typeRepository.Update(isType);

            return JsonHelper.Serialize(new {
                Code = 200,
                Data = isType,
                Msg = "修改成功"
            });            
        }


        //删除分类
        // Put /commoditytype/id
        [HttpDelete("{id}")]
        public dynamic DeleteType(int id)
        {

            var isType = _typeRepository.GetById(id);

            isType.IsDelete = true;

            _typeRepository.Update(isType);

            return JsonHelper.Serialize(new {
                Code = 200,
                Data = isType,
                Msg = "删除成功"
            });            
        }



        
    }
}
